package Servlet;

import bean.ChatGroup;
import bean.Message;
import bean.User;
import dao.ChatGroupDao;
import dao.MessageDao;
import dao.UserDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "addQQ", value = "/addQQ")
public class addQQ extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        String myID = request.getParameter("myID");
        System.out.println(myID);
        System.out.println(id);
        ChatGroupDao chatGroupDao = new ChatGroupDao();
        ChatGroup chatGroup = new ChatGroup(id);
        User user = new User();
        user.setId(myID);
        MessageDao messageDao = new MessageDao();
        PrintWriter out = response.getWriter();
        Map map = new HashMap();
        boolean flag =  chatGroupDao.addMember(chatGroup , user);
        if(flag){
            ChatGroup chatGroupDao1 =  chatGroupDao.findGroup(id);
            map.put("chatGroup",chatGroupDao1);
            List<User> users = chatGroupDao.listMember(chatGroupDao1);
            map.put("users",users);
            List<Message> messages = messageDao.listAllChatGroupMassage(chatGroupDao1);
            map.put("message",messages);
            out.print(JSONObject.fromObject(map));
        }else{
            Object chatGroupDao1 = null;
            out.print(JSONObject.fromObject(chatGroupDao1));
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
